package com.welike.shibo.controller.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.welike.shibo.controller.BaseController;
import com.welike.shibo.eneity.JsonResult;
import com.welike.shibo.service.AudioService;
import com.welike.shibo.utils.ResultUtils;
import com.welike.shibo.vo.page.AudioPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author fangyu
 * @version v1.0.0
 * @since 2019-08-21 03:14
 */

@Slf4j
@Api(value = "音频相关接口")
@RestController
@RequestMapping("/api/audio")
public class ApiAudioController extends BaseController {

    @Autowired
    private AudioService audioService;

    @ApiOperation(value = "查询所有的音乐", notes = "查询所有的音乐")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "page", value = "页码", required = false),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "分页容量", required = false)})
    @GetMapping("/list/all")
    public JsonResult getAudioList(
            @RequestParam(value = "page", required = false, defaultValue = "1") int page,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize
    ){
        map.clear();
        map.put("path", "/api/audio/list/all");
        Integer state = 1;
        IPage<AudioPageVo> iPage = audioService.selectAudioPageVo(page, pageSize, state);
        map.put("indexList", iPage.getRecords());
        map.put("page", page);
        map.put("pageSize", pageSize);
        return ResultUtils.success(map);
    }


    @ApiOperation(value = "上传音乐", notes = "用户上传音乐")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "file", value = "音乐文件", required = true),
            @ApiImplicitParam(paramType = "query", name = "name", value = "音乐名", required = true),
            @ApiImplicitParam(paramType = "query", name = "duration", value = "音乐时长", required = true)
    })
    @PostMapping("/publish")
    public JsonResult publish(
            @RequestParam(value = "file") MultipartFile file,
            @RequestParam(value = "name") String name,
            @RequestParam(value = "duration") Integer duration,
            HttpServletRequest request
    ) throws IOException {
        return audioService.uploadMusic(file, name, duration);
    }

}
